<?php
/**
 * Created by PhpStorm
 * Author: 沁塵
 * Date: 2020/9/5
 * Time: 6:09 下午
 */

namespace app\api\service;



use app\api\model\VideoDoulaoban;
use app\api\model\VideoProduct;
use app\api\model\VideoProductVideos;
use app\api\service\token\LoginToken;

class VideoService
{
    /*
     *
     * 添加商品视频
     * */
    public function addProductVideos($data)
    {
        $douyin_pid = $data['douyin_pid'];
        $douyin_product = VideoProduct::where('douyin_pid',$douyin_pid)->findOrEmpty();
        $count = 0;
        if($douyin_product->isEmpty()){
            // 新增商品
            VideoProduct::create([
                "douyin_pid" => $data['douyin_pid'],
                "douyin_pname" => $data['douyin_pname'],
                "douyin_purl" => $data['douyin_purl'],
                "jd_pid" => $data['jd_pid'],
                "tb_pid" => $data['tb_pid'],
                "category" => $data['category'],
                "status" => 3
            ]);
        }

        if(count($data['videos']) > 0){
            foreach ($data['videos'] as $video){
                $is_exists = VideoProductVideos::where('content_id',$video['content_id'])->findOrEmpty();
                if($is_exists->isEmpty()){
                    $count ++;
                    $video['douyin_pid'] = $douyin_pid;
                    VideoProductVideos::create($video);
                }
            }
        }

        return $count;

    }

    public function addDoulaobanProduct($data)
    {
        $data = $data['data'];
        $is_arr = is_array($data);
        if (!$is_arr){
            $data = json_decode($data,true);
        }

        $count = 0;


        if(count($data) > 0){
            foreach ($data as $p){
                $is_exists = VideoDoulaoban::where('douyin_pid',$p['douyin_pid'])->findOrEmpty();
                if($is_exists->isEmpty()){
                    $count ++;
                    VideoDoulaoban::create($p);
                }
            }
        }

        return $count;
    }
}